﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UtilityLibrary;
using FarseerPhysics.Dynamics.Joints;

namespace MapLibrary.Players.Skills
{
    class Net : Skill
    {
        public const int MAX_LEVEL = 3;
        /// <summary>
        /// remaining frames until skill can be activated
        /// </summary>
        private int cooldown;

        /// <summary>
        /// amount of frames to wait after last shot
        /// </summary>
        private int reuseTime;

        /// <summary>
        /// velocity to fire the net
        /// </summary>
        private float velocity;

        private LevelDepValue<int> lifetime;

        public Net() : base()
        {
            reuseTime = 100;
            velocity = 10.0f;

            SetLevelThresholds();
        }

        public override bool ActivateConditionsSatisfied(Player p)
        {
            return cooldown == 0;
        }

        public override void Activate(Player p)
        {
            cooldown = reuseTime;

            NetSprite n;
            if (level >= 2)
            {
                n = p.World.ProjectileManager.AddLargeNet(p, velocity) as NetSprite;
            }
            else
            {
                n = p.World.ProjectileManager.AddNet(p, velocity) as NetSprite;
            }

            n.TimeToLive = lifetime.Value;
        }

        public override void Update()
        {
            cooldown--;
            if (cooldown < 0)
            {
                cooldown = 0;
            }
        }

        private void SetLevelThresholds()
        {
            lifetime = new LevelDepValue<int>(this);
            lifetime.AddValueForLevel(1, 500);
            lifetime.AddValueForLevel(3, 1000);
        }

        public int LifeTime()
        {
            return lifetime.Value;
        }

        public override void DrawAtPlayer(SpriteBatch batch, WorldViewport viewport, Player p)
        {
            Vector2 position = viewport.WorldToViewportCoords(p.Position);
            position = position - p.GetCenter() - new Vector2(0, 75);

            if(cooldown == 0){
                PrimitiveBrush.RenderText(batch, "Net ready", position);
            }
            else{
                PrimitiveBrush.RenderText(batch, "Net not ready", position);
            }
        }

        public override int GetAmount(string trackerName)
        {
            return reuseTime - cooldown;
        }

        public override int GetMax(string trackerName)
        {
            return reuseTime;
        }
    }
}
